package ModelLayer;
import java.util.*;



/**
 * Write a description of class AdressBookCon here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AddressBookCon
{
   private ArrayList<Person> addressBook;
   private static AddressBookCon instance;
   static int runningAddrId;

    /**
     * Constructor for objects of class AdressBookCon
     */
    private AddressBookCon()
    {
        addressBook = new ArrayList <Person>();
         if (runningAddrId < 0)
            {
                runningAddrId = 0;
            }
    }
    
    /**
     * Singelton method for working with only one instance of AddressBookCon
     */
    public static AddressBookCon getInstance()
    {
       if(instance == null)
           {
               instance = new AddressBookCon();
           }
       return instance;
    }
    
    /**
     * Creates Person objects and adding them to AddressBookCon 
     */ 
    public void createPerson(String name, String address, String postalCode, String city, String phone)
    {
        Person newPerson = new Person(name, address, postalCode, city, phone);
        addPerson(newPerson);
    }
    
    /**
     * Mutator method 
     */
    public void updatePerson(Person p, String name, String address, String postalCode, String city, String phone)
    {
        p.updatePerson(name, address, postalCode, city, phone);
    }
    
    /**
     * adds Person objects to AddressBookCon
     */
    public void addPerson(Person newPerson)
    {
        addressBook.add(newPerson);
    }
    
    /**
     * removes Person objects from AddressBookCon 
     */
    public void deletePerson(long id)
    {
        addressBook.remove(getPerson(id));
    }
    
    /**
     * Prints information about Person objects from AddressBookCon
     */
    public void getAddressBook()
    {
        for (Person person : addressBook)
        {
            System.out.println("id: " + person.getId() + "\t name: " + person.getName());
        }
    }
    
    /**
     * Returns an index nummer of  a corresponding id or the next avaiable index
     */
    public int findPersonIndex(long id)
    {
        int index = 0;
        boolean found = false;
        while(index < addressBook.size() && !found)
        {            
            if(id == addressBook.get(index).getId())
            {
                found = true;
            }
                
            else
            {
                index++;
            }       
            
            }  
        
         if (!found)
            {
                index = addressBook.size()+1;
            }
            
        return index;
    }
    
    /**
     * Returns a Person object from an id search
     */
    public Person getPerson(long id)
    {
        
        int index = findPersonIndex(id);
        if(index < addressBook.size())
        {
            return addressBook.get(index);
        }
        else 
        {
            Person person = null;
            return person;
        }

    }
   
    
}
